﻿using LinqKit;
using Management.Auxiliary;
using System.Linq;

namespace Management.Core.Permission
{
    public class PermissionManager : BaseManager<Permission>
    {
        public Paging<Permission> FindPageList(Paging<Permission> pagingPermission, string controllerName, string actionName, int? order)
        {
            //查询表达式
            var _where = PredicateBuilder.New<Permission>(true);
            if (!string.IsNullOrEmpty(controllerName)) _where = _where.And(u => u.ControllerName.Contains(controllerName));
            if (!string.IsNullOrEmpty(actionName)) _where = _where.And(u => u.ActionName.Contains(actionName));
            //排序
            OrderParam _orderParam;
            switch (order)
            {
                case 0://ID升序
                    _orderParam = new OrderParam() { PropertyName = "PermissionID", Method = OrderMethod.ASC };
                    break;
                case 1://ID降序
                    _orderParam = new OrderParam() { PropertyName = "PermissionID", Method = OrderMethod.DESC };
                    break;
                case 2://创建时间降序
                    _orderParam = new OrderParam() { PropertyName = "Created_at", Method = OrderMethod.ASC };
                    break;
                case 3://创建时间升序
                    _orderParam = new OrderParam() { PropertyName = "Created_at", Method = OrderMethod.DESC };
                    break;
                default://ID降序
                    _orderParam = new OrderParam() { PropertyName = "PermissionID", Method = OrderMethod.DESC };
                    break;
            }
            pagingPermission.Items = Repository.FindPageList(pagingPermission.PageSize, pagingPermission.PageIndex, out pagingPermission.TotalNumber, _where, _orderParam).ToList();
            return pagingPermission;
        }
    }
}
